package com.lanou.hotel.controller;

import com.lanou.hotel.entity.Guests;
import com.lanou.hotel.entity.Home;
import com.lanou.hotel.entity.Type;
import com.lanou.hotel.entity.response.GuestsItem;
import com.lanou.hotel.service.IGuestsService;
import com.lanou.hotel.service.IHomeService;
import com.lanou.hotel.service.ITypeService;
import com.lanou.hotel.utils.UUIDUtil;
import com.lanou.hotel.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author msy
 * @since 2023-10-29
 */
@RestController
public class GuestsController {

    @Autowired
    private IGuestsService guestsService;
    @Autowired
    private IHomeService homeService;
    @Autowired
    private ITypeService typeService;

    /**
     * 初始化租客页面
     * @return
     */
    @GetMapping("/guests/init")
    public ResultVO<Map<String,Object>> init(){
        List<Home> homeList = homeService.list();
        List<Type> typeList = typeService.list();

        if (homeList != null && typeList != null) {
            Map<String, Object> map = new HashMap<>();

            map.put("homeList",homeList);
            map.put("typeList",typeList);

            return ResultVO.success("请求成功",map);
        }else {
            return ResultVO.failed("系统繁忙，请稍后重试...");
        }
    }

    /**
     * 分页条件查询 租客
     * @param name
     * @param sex
     * @param card
     * @param phone
     * @param startTime
     * @param endTime
     * @param num
     * @param typeId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @PostMapping("/guests/getGuests")
    public ResultVO<Map<String,Object>> getGuests(
        String name,
        String sex,
        String card,
        String phone,
        String startTime,
        String endTime,
        Integer num,
        Integer typeId,
        Integer pageNum,
        Integer pageSize
    ){
        Guests guests = new Guests(null,name,sex,card,phone,null,num,typeId);
        return guestsService.getGuests(guests,startTime,endTime,pageNum,pageSize);
    }

    /**
     * 保存已修改的租客
     * @param guests
     * @return
     */
    @PostMapping("/guests/saveGuest")
    public ResultVO saveGuest(@RequestBody Guests guests){
        //通过房号获取房类型id
        Integer num = guests.getNum();
        Home home = homeService.getOneByNum(num);

        //修改租客房间类型id
        int typeId = home.gethTypeId();
        guests.settId(typeId);

        boolean isUpdate = guestsService.updateById(guests);
        if (isUpdate) {
            return ResultVO.success("修改成功",null);
        }else{
            return ResultVO.failed("系统繁忙,请稍后重试...");
        }
    }

    /**
     * 添加租客信息
     * @param guests
     * @return
     */
    @PostMapping("/guests/addGuest")
    public ResultVO addGuest(@RequestBody Guests guests){
        guests.setId(UUIDUtil.getUUID());
        boolean isSave = guestsService.save(guests);

        if (isSave) {
            return ResultVO.success("添加租客成功",null);
        }else {
            return ResultVO.failed("系统繁忙,请稍后重试...");
        }
    }

    @PostMapping("/guests/deleteGuests")
    public ResultVO deleteGuests(@RequestBody List<GuestsItem> guestsItemList){

        boolean isDel = guestsService.removeBatchByIds(guestsItemList);

        if (isDel) {
            return ResultVO.success("删除成功",null);
        }else{
            return ResultVO.failed("系统繁忙,请稍后重试...");
        }
    }
}
